package com.zhangcl.timertest;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 1.实现2秒爆炸一次,一直循环
 * 2.实现2秒爆炸一次,4秒爆炸一次,依次2秒,4秒循环
 * @author zhangchunliang
 *
 */
public class TraditionaryTimerTest {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		twoBombing();
//		twoAndFourBombing();
		twoAndFourBombingEx();
		while (true) {
			try {
				Thread.sleep(1000);
				System.out.println(String.valueOf(new Date().getSeconds()));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 实现2秒爆炸一次,一直循环
	 */
	private static void twoBombing() {
		new Timer().schedule(new TraditionaryTimerTest().new MyTimerTask(),
				2000, 2000);
	}
	
	public class MyTimerTask extends TimerTask {
		
		public MyTimerTask(){};
		@Override
		public void run() {
			System.out.println("Bombing...");
		}
		
	} 
	
	/**
	 * 实现2秒爆炸一次,4秒爆炸一次,依次2秒,4秒循环
	 */
	private static void twoAndFourBombing(){
		new Timer().schedule(new TraditionaryTimerTest().new MyTimerTaskEx(), 2000);
	}
	private static int blog = 0;
	public class MyTimerTaskEx extends MyTimerTask{
		@Override
		public void run() {
			blog++;
			int count = blog%2;
			System.out.println("Bombing...");
			new Timer().schedule(new MyTimerTaskEx(), 2000+2000*count);
		}
	}
	
	private static void twoAndFourBombingEx(){
		new Timer().schedule(new TraditionaryTimerTest().new oneTimerTask(), 2000);
	}
	
	public class oneTimerTask extends TimerTask{

		@Override
		public void run() {
			System.out.println("Bombing...");
			extureSchedule(4000);
			
		}
		
	}
	
	public class twoTimerTask extends TimerTask{
		
		@Override
		public void run() {
			System.out.println("Bombing...");
			extureSchedule(2000);
		}
		
	}
	
	public void extureSchedule(int time){
		new Timer().schedule(new oneTimerTask(), time);
	}
}
